package com.book.controller;

import com.book.entity.BookInfo;
import com.book.service.BookInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author SuperYu  2024/6/17
 */
@Controller
@Slf4j
public class IndexController {
    @Autowired
    private BookInfoService bookInfoService;

    @GetMapping("/toIndex")
    @RequestMapping("/")
    public ModelAndView Index() {
        log.info("------ index ------");
        ModelAndView modelAndView = new ModelAndView("index");
        try {
            List<BookInfo> bookInfos = bookInfoService.selectBookInfoByBookSaleCount(8);
            modelAndView.addObject("goodBookHotSellList", bookInfos);
        } catch (Exception e) {
            e.printStackTrace();
            return modelAndView;
        }
        return modelAndView;
    }

    @GetMapping("/index")
    public String toIndex(){
        log.info("------ index ------");
        return "index";
    }
    @GetMapping("/newIndex")
    public String logout(HttpSession session) {
        // 清除session中的登录信息
        session.invalidate();
        // 重定向到index.html页面
        return "redirect:/index";
    }
    @GetMapping("/toLogin")
    public String toLogin(){
        log.info("------ to login ------");
        return "login";
    }

    @GetMapping("/toLogout")
    public String toLogout(HttpSession session) {
        log.info("------ to logout ------");
        session.setAttribute("loginName", null);
        return "login";
    }

}
